package com.biz.aoao.mysql.mysqlbinlogsync.test;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.biz.aoao.mysql.mysqlbinlogsync.util.HttpUtil;
import com.github.shyiko.mysql.binlog.BinaryLogClient;
import com.github.shyiko.mysql.binlog.event.*;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class App {
    public static void sync() throws IOException
    {
        BinaryLogClient client = new BinaryLogClient("hk-cdb-qmknzqmh.sql.tencentcdb.com", 63684, "root", "hH123456789");
        client.setBinlogFilename("mysql-bin.000018");
        client.setBinlogPosition(204873763);  //从指定的位置读取binlog，具体位置可以在mysql中查
   /*   EventDeserializer eventDeserializer = new EventDeserializer();
        eventDeserializer.setCompatibilityMode(
            EventDeserializer.CompatibilityMode.DATE_AND_TIME_AS_LONG,
               EventDeserializer.CompatibilityMode.CHAR_AND_BINARY_AS_BYTE_ARRAY
      );*/
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    /*    client.setEventDeserializer(eventDeserializer);*/
        client.registerEventListener(new BinaryLogClient.EventListener() {
            @Override
            public void onEvent(Event event) {
                EventData data = event.getData();
                EventHeader header = event.getHeader();
                Date date=new Date(header.getTimestamp());
                if(header.getEventType().name().equals("TABLE_MAP")){
                    JSONObject jsonObject = JSON.parseObject(JSON.toJSON(data).toString());
                    if(jsonObject.get("database").equals("work") && jsonObject.get("table").equals("member_transaction")){
                        System.out.println(date);
                        System.out.println(jsonObject);
                        HttpUtil.sync();
                    }
                }
            }
        });
        client.connect();
    }
}
